<?php
header("content-type:text/html;charset=utf-8");
$mysqli = new mysqli("localhost", "root", "123", "my_test");
$mysqli->set_charset("utf8");
$id = $_GET['id'];
$username = $_POST['username'];
$password = $_POST['password'];
$age = $_POST['age'];

//echo $id.$username.$password.$age;


$query = "SELECT * FROM users";
$res = $mysqli->query($query);


$action = $_GET['action'];
switch ($action) {
    case "add":
        if (!isset($username) || empty($username)) {
            echo "用户名不能为空, 2秒后跳回...";
            echo "<script>setTimeout(function() {window.location.href = 'user_add.php?username=$username&age=$age';},2000)</script>";
            exit();
        }
        if (!isset($password) || empty($password)) {
            echo "密码不能为空, 2秒后跳回...";
            echo "<script>setTimeout(function() {window.location.href = 'user_add.php?username=$username&age=$age';},2000)</script>";
            exit();
        }
        if (!isset($age) || empty($age)) {
            echo "年龄不能为空, 2秒后跳回...";
            echo "<script>setTimeout(function() {window.location.href = 'user_add.php?age=$age&username=$username';},2000)</script>";
            exit();
        }

        $username = $mysqli->escape_string($username);

        $query = "INSERT INTO users(username, password, age) VALUES('$username','$password','$age')";
        if ($mysqli->query($query)) {
            echo '注册成功!';
            echo "<script>setTimeout(function() {window.location.href = 'user_list.php';},2000)</script>";
        }
        break;
    case "del":
        $query = "DELETE FROM users  WHERE id=$id";
        if ($mysqli->query($query)) {
            if ($mysqli->affected_rows) {
                echo "<script>alert('删除成功!');window.location.href = 'user_list.php'</script>";
            }
        }

        break;
    case "edit":
//        $query = "SELECT * FROM users WHERE id = $id";
//        $mysqli->query()

        if (!isset($username) || empty($username)) {
            echo "用户名不能为空, 2秒后跳回...";
            echo "<script>setTimeout(function() {window.location.href = 'user_add.php?username=$username&age=$age';},2000)</script>";
            exit();
        }
        if (!isset($password) || empty($password)) {
            echo "密码不能为空, 2秒后跳回...";
            echo "<script>setTimeout(function() {window.location.href = 'user_add.php?username=$username&age=$age';},2000)</script>";
            exit();
        }
        if (!isset($age) || empty($age)) {
            echo "年龄不能为空, 2秒后跳回...";
            echo "<script>setTimeout(function() {window.location.href = 'user_add.php?age=$age&username=$username';},2000)</script>";
            exit();
        }

        $username = $mysqli->escape_string($username);

        $query = "UPDATE users SET username='$username',password='$password',age=$age WHERE id=$id";
        if ($mysqli->query($query)) {
            echo '更新成功!';
            echo "<script>setTimeout(function() {window.location.href = 'user_list.php';},2000)</script>";
        }
        break;
    case "del":
        $query = "DELETE FROM users  WHERE id=$id";
        if ($mysqli->query($query)) {
            if ($mysqli->affected_rows) {
                echo "<script>alert('删除成功!');window.location.href = 'user_list.php'</script>";
            }
        }
        break;
    default:
        echo "我不知道你要干什么";
}

?>